<template>
  <div class="anquanrenzheng">
    <van-nav-bar title="修改手机号" left-arrow @click-left="$router.back()" />
    <div class="title">为保障您的账户安全，更换手机号前需要安全验证</div>
    <van-cell-group inset class="form">
      <van-field label="当前手机号" :model-value="data.userData.tel" readonly />
      <van-field
        label="新手机号"
        v-model="data.edit.mobile"
        placeholder="请输入新手机号"
      />
      <van-field
        label="验证码"
        v-model="data.smsCode"
        center
        clearable
        placeholder="请输验证码"
      >
        <template #right-icon>
          <button
            class="a"
            v-if="data.send == false"
            :disabled="data.send"
            @click="getsmsCode"
          >
            获取验证码
          </button>
          <van-count-down
            class="b"
            v-else
            :time="data.time"
            format="ss秒"
            @finish="finish"
          />
        </template>
      </van-field>
    </van-cell-group>
    <div class="btnbox">
      <button @click="setTel" class="btn">确定修改</button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import {
  getUserInfoApi,
  getSmsCodeyzmApi,
  setMobileApi,
  setMobileApi2
} from '@/http/api'
import { useCounterStore } from '@/stores/counter'
import { Toast } from 'vant'
import { useRouter } from 'vue-router'
const store = useCounterStore()
const router = useRouter()
onMounted(() => {
  getUserInfo()
})
const data = reactive({
  userData: {},
  edit: {
    mobile: ''
  },
  smsCode: '',
  send: false,
  time: 10 * 1000
})
const getUserInfo = async () => {
  const res = await getUserInfoApi(store.res.id)
  console.log(res)
  data.userData = res
  console.log(data.userData)
}

const getsmsCode = () => {
  data.send = true

  let obj = {
    mobile: data.edit.mobile,
    sms_type: 'editmobile'
  }
  getSmsCodeData(obj)
}
// 获取验证码接口
const getSmsCodeData = async (obj) => {
  const res = await getSmsCodeyzmApi(obj)
}

// 倒计时结束
const finish = () => {
  data.send = false
  data.time = 10 * 1000
}

const setTel = async () => {
  let obj = {
    current_mobile: data.userData.tel,
    new_mobile: data.edit.mobile,
    sms_code: data.smsCode,
    sms_type: 'editmobile'
  }
  console.log(obj)
  const res = await setMobileApi2(obj)
  console.log(res)
  Toast('设置成功，请重新登录！')
  router.replace('/login')
}
</script>
<style lang="scss" scoped>
.anquanrenzheng {
  .title {
    background-color: #f8f8f8;
    font-size: 13px;
    color: #666;
    text-align: center;
    padding: 40px 0;
  }
}
.van-field__button {
  padding-left: var(--van-padding-xs);
  width: 80px;
  height: 60px;
  margin-top: -30px;
}
.btnbox {
  margin-top: 80px;
  padding: 0 40px;
  box-sizing: border-box;
  .btn {
    width: 100%;
    font-size: 16px;
    line-height: 80px;
    color: #fff;
    border: 1px solid #f15a24;
    background-color: #f15a24;
    border-radius: 10px;
  }
}
.a {
  width: 150px;
  height: 60px;
  color: #fff;
  background-color: #f15a24;
  border-radius: 10px;
  border: 1px solid #f15a24;
  font-size: 12px;
}
.b {
  margin-top: -40px;
}
</style>
